package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.RelicsHistoricSitesArchitecture;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.RelicsHistoricSitesCurrentAssets;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ReportPublicField;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.RelicsHistoricSitesRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.strategy.StrategyService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/16
 * @Description: 文物古迹地震损失报表
 */
//@Service("文物古迹地震损失报表")
@Service("17")
public class RelicsHistoricSitesServiceImpl implements StrategyService {
    @Resource
    private RelicsHistoricSitesRepository relicsHistoricSitesRepository;
    @Resource
    ReportPublicFieldRepository reportPublicFieldRepository;
    
    @Override
    public void reportImport(Workbook workbook, String xlrmId, String previousXlrmId, ReportPublicField reportPublicField) {
        if(!PlatformObjectUtils.isEmpty(previousXlrmId)){
            relicsHistoricSitesRepository.deleteByXlrmId(previousXlrmId);
            //删除报表公共字段
            reportPublicFieldRepository.deleteByXlrmId(previousXlrmId);
        }
        Map map = getList(workbook,xlrmId);
        List<RelicsHistoricSitesArchitecture> architectureList = (List<RelicsHistoricSitesArchitecture>) map.get("architectureList");
        List<RelicsHistoricSitesCurrentAssets>  currentAssetsList= (List<RelicsHistoricSitesCurrentAssets>) map.get("currentAssetsList");
        relicsHistoricSitesRepository.batchArchitectureSave(architectureList);
        relicsHistoricSitesRepository.batchCurrentSave(currentAssetsList);
        reportPublicField.setProvince(architectureList.get(0).getProvince());
        reportPublicField.setCity(architectureList.get(0).getCity());
        reportPublicField.setCounty(architectureList.get(0).getCounty());
        reportPublicField.setTownship(architectureList.get(0).getTownship());
        reportPublicField.setEnterpriseName(architectureList.get(0).getEnterpriseName());
        //默认删除
        reportPublicField.setDelFlag(YNEnum.Y.toString());
        reportPublicFieldRepository.save(reportPublicField);
    }
    //获取list
    private Map getList(Workbook workbook, String xlrmId){
        HashMap map = new HashMap<>();
        //企业地震损失报表_固定资产部分
        List<RelicsHistoricSitesArchitecture> architectureList = new ArrayList<>();
        //企业地震损失报表_流动资产部分
        List<RelicsHistoricSitesCurrentAssets> currentAssetsList = new ArrayList<>();
        if (workbook != null) {
            //循环除了第一行的所有行
            /** 得到第二个sheet */
            Sheet sheet = workbook.getSheetAt(0);
            /** 得到Excel的行数 */
            int totalRows = sheet.getPhysicalNumberOfRows();
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            //填表人
            String fillFormUser = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(1));
            //联系电话
            String tel = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(3));
            //填表日期
            String fillFormTime = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(5));
            //省
            String province = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(1));
            //市
            String city = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(3));
            //县
            String county = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(5));
            //乡
            String township = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(7));
            //企业名称
            String enterpriseName = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(9));
            String type=null;
            String assetType=null;

            /** 循环Excel的行 从第4行开始*/
            for (int r = 3; r < totalRows; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                if (hSSFDataFormatter.formatCellValue(row.getCell(0)).equals("建（构）筑物")){
                    type=hSSFDataFormatter.formatCellValue(row.getCell(0));
                }
                if (hSSFDataFormatter.formatCellValue(row.getCell(0)).equals("流动资产")){
                    type=hSSFDataFormatter.formatCellValue(row.getCell(0));
                }
                //固定资产部分
                if (type.equals("建（构）筑物")){
                    if (hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("名称\n" +
                            "（不超过100字）")){
                        continue;
                    }
                    RelicsHistoricSitesArchitecture architecture = new RelicsHistoricSitesArchitecture();
                    //主键id
                    architecture.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    //报表主表id
                    architecture.setXlrmId(xlrmId);
                    //填表人
                    architecture.setFillFormUser(fillFormUser);
                    //tel
                    architecture.setTel(tel);
                    //填报日期
                    architecture.setFillFormTime(fillFormTime);
                    //省
                    architecture.setProvince(province);
                    //市
                    architecture.setCity(city);
                    //县
                    architecture.setCounty(county);
                    //乡
                    architecture.setTownship(township);
                    //公司名称
                    architecture.setEnterpriseName(enterpriseName);
                    //资产分类
                    if (!hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("")){
                        assetType=hSSFDataFormatter.formatCellValue(row.getCell(1));
                    }
                    architecture.setType(assetType);
                    //名称
                    architecture.setName(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                    //建造时间
                    architecture.setConstructionTime(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                    //保护级别
                    architecture.setProtectionLevel(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                    //建造面积（m2）
                    architecture.setFloorArea(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                    //结构类型
                    architecture.setStructureType(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                    //破坏状态描述
                    architecture.setDamageConditionDesc(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                    //破坏等级
                    architecture.setDestroyLevel(hSSFDataFormatter.formatCellValue(row.getCell(8)));
                    //损失/万元
                    architecture.setLoss(hSSFDataFormatter.formatCellValue(row.getCell(9)));
                    //恢复方式
                    architecture.setRecoveryMode(hSSFDataFormatter.formatCellValue(row.getCell(10)));
                    //恢复投资/万元
                    architecture.setRestoreMoney(hSSFDataFormatter.formatCellValue(row.getCell(11)));
                    //序列
                    architecture.setSequence(String.valueOf(r));
                    architectureList.add(architecture);
                }
                //流动资产部分
                if (type.equals("流动资产")){
                    if (hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("名称\n" +
                            "（不超过100字）")){
                        continue;
                    }
                    RelicsHistoricSitesCurrentAssets currentAssets = new RelicsHistoricSitesCurrentAssets();
                    currentAssets.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    currentAssets.setXlrmId(xlrmId);
                    //资产分类
                    if (!hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("")){
                        assetType=hSSFDataFormatter.formatCellValue(row.getCell(1));
                    }
                    currentAssets.setType(assetType);
                    //名称
                    currentAssets.setName(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                    //总价值(万元)
                    currentAssets.setTotalValue(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                    //破坏状态描述
                    currentAssets.setDamageConditionDesc(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                    //损失/万元
                    currentAssets.setLoss(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                    //恢复投资/万元
                    currentAssets.setRestoreMoney(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                    //序列
                    currentAssets.setSequence(String.valueOf(r));
                    //省
                    currentAssets.setProvince(province);
                    //市
                    currentAssets.setCity(city);
                    //县
                    currentAssets.setCounty(county);
                    currentAssetsList.add(currentAssets);
                }
            }
        }
        map.put("architectureList",architectureList);
        map.put("currentAssetsList",currentAssetsList);
        return map;
    }
}
